package com.zxn.ssyx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxn.ssyx.common.exception.SsyxException;
import com.zxn.ssyx.common.result.ResultCodeEnum;
import com.zxn.ssyx.mapper.RegionWareMapper;
import com.zxn.ssyx.model.sys.RegionWare;
import com.zxn.ssyx.service.RegionWareService;
import com.zxn.ssyx.vo.sys.RegionWareQueryVo;
import jodd.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegionWareServiceImpl extends ServiceImpl<RegionWareMapper, RegionWare> implements RegionWareService {
    @Autowired
    private RegionWareMapper regionWareMapper;

    @Override
    public IPage<RegionWare> selectPage(Page<RegionWare> pageParam, RegionWareQueryVo regionWareQueryVo) {
        LambdaQueryWrapper<RegionWare> lqw = new LambdaQueryWrapper<>();
        if (regionWareQueryVo.getKeyword() != null) {
            lqw.like(RegionWare::getRegionName, regionWareQueryVo.getKeyword())
                    .like(RegionWare::getWareName, regionWareQueryVo.getKeyword());
        }
        return regionWareMapper.selectPage(pageParam, lqw);
    }

    @Override
    public void saveRegionWare(RegionWare regionWare) {
        RegionWare one = lambdaQuery().eq(RegionWare::getRegionId, regionWare.getRegionId()).one();
        if (one != null) {
            throw new SsyxException(ResultCodeEnum.REGION_OPEN);
        }
        regionWareMapper.insert(regionWare);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        lambdaUpdate().set(RegionWare::getStatus,status)
                .eq(RegionWare::getId,id);
    }
}
